package com.tiyende.kash.loop.ui.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.TiyendeApp
import com.tiyende.kash.loop.common.CommonDialog
import com.tiyende.kash.loop.databinding.TiyendeDialogCityBinding
import com.tiyende.kash.loop.event.TiyendeEventPointType
import com.tiyende.kash.loop.weight.pickerview.adapter.ArrayWheelAdapter
import com.tiyende.kash.loop.weight.wheelview.view.WheelView

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：城市选择dialog
 */
class TiyendeChooseDialog: CommonDialog {

    private lateinit var binding: TiyendeDialogCityBinding
    private var mCallback: IDialogCallback? = null
    private var dataList: ArrayList<String>? = null
    private var selectedIndex = 0
    //事件备注
    private var eventRemark = ""

    override fun initTiyendeWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        eventRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogCityBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initTiyendeView() {
        if (dataList == null){
            dataList = arrayListOf()
        }
        val centerColors = intArrayOf(Color.parseColor("#ff2e9b95"), Color.parseColor("#ff2c366b"))
        val outColors = intArrayOf(Color.parseColor("#ffb0e1e2"), Color.parseColor("#ff329f97"))
        binding.cityView.setTextColorCenter(Color.parseColor("#FF3ED9AB"))
        binding.cityView.setTextColorOut(Color.parseColor("#FFCBD5E1"))
        binding.cityView.setLineSpacingMultiplier(3.5f)
        binding.cityView.setItemsVisibleCount(5)
        binding.cityView.setAlphaGradient(true)
        binding.cityView.setLabel("")
        binding.cityView.setGravity(Gravity.CENTER)
        binding.cityView.setTypefaceCenter(TiyendeApp.app.getBlackTypeface())
        binding.cityView.setTypefaceOut(TiyendeApp.app.getMediumTypeface())
        binding.cityView.setTypefaceLable(Typeface.createFromAsset(context.assets,"fonts/Roboto-Regular.ttf"))
        binding.cityView.setDividerColor(Color.parseColor("#00000000"))
        binding.cityView.setDividerType(WheelView.DividerType.WRAP)
        binding.cityView.adapter = ArrayWheelAdapter(dataList)
        binding.cityView.setCyclic(true)
        binding.cityView.setCurrentItem(0)
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.btnDone) //ok
        binding.cityView.setOnItemSelectedListener { index ->
            selectedIndex = index
        }
    }

    override fun loadTiyendeData() {
    }

    override fun onTiyendeViewClick(v: View?) {
        dismiss()
        dataList?.let { list ->
            if (list.size >= selectedIndex){
                mCallback?.onTiyendeSelectedItem(list[selectedIndex])
            }
        }
    }

    /**
     * 设置埋点备注
     */
    fun addTiyendeTipsEventRemark(remark: String){
        eventRemark = remark
    }

    /**
     * 设置提示title
     */
    fun addTiyendeTitleText(title: String){
        binding.txtTipsTitle.text = title
    }

    /**
     * 设置提示title
     */
    fun addTiyendeTitleText(title: String, color: Int){
        addTiyendeTitleText(title)
        binding.txtTipsTitle.setTextColor(color)
    }

    /**
     * 设置ok文字
     */
    fun addTiyendeBtnSubmitText(text: String){
        binding.btnDone.text = text
    }

    /**
     * 设置数据
     */
    fun addTiyendeDataList(list: ArrayList<String>){
        dataList?.clear()
        dataList?.addAll(list)
    }

    interface IDialogCallback {
        fun onTiyendeSelectedItem(cityName: String)
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(String.format(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_CHOOSE_SHOW,eventRemark))
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(String.format(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_CHOOSE_SHOW,eventRemark))
    }
}